package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
@Api(tags = "地址相关接口")
public class AddressBookController {
 @Autowired
 private AddressBookService addressBookService;
@GetMapping("/list")
@ApiOperation(value = "分页查询接口")
 public R<List<AddressBook>> getAddressBooks(AddressBook addressBook){
 R<List<AddressBook>> addressBookList = addressBookService.getAddressBooks(addressBook);
 return addressBookList;
 }
 @PostMapping
 @ApiOperation(value = "新增接口")
 public R<String> save(@RequestBody AddressBook addressBook){
  log.info("addressBook:{}", addressBook);
  R<String> stringR = addressBookService.save1(addressBook);

  return  stringR;
 }
 @PutMapping
 @ApiOperation(value = "更新接口")
 public R<String> upDateAddress(@RequestBody AddressBook addressBook){
  R<String> stringR = addressBookService.upDateAddress(addressBook);
  return stringR;
 }
 @GetMapping("default")
 @ApiOperation(value = "获取默认地址接口")
 public R<AddressBook> getDefaultAddressBook(){
  R<AddressBook> defaultAddressBook = addressBookService.getDefaultAddressBook();
  return defaultAddressBook;
 }
 @GetMapping("/{id}")
 @ApiOperation(value = "返显接口")
 public R get(@PathVariable Long id) {
  AddressBook addressBook = addressBookService.getById(id);
  if (addressBook != null) {
   return R.success(addressBook);
  } else {
   return R.error("没有找到该对象");
  }
 }

 @PutMapping("default")
 @ApiOperation(value = "设置默认地址接口")
 public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
  log.info("addressBook:{}", addressBook);
  R<AddressBook> addressBookR = addressBookService.setDefault(addressBook);
  return addressBookR;
 }
}
